<?php


namespace app\api\service;


use app\common\model\Config;

class Message{

    protected $access_token;
    protected $touser;
    protected $template_id;
    protected $form_id;

    public function __construct($template_id){
        $accessToken = new Accesstoken();
        $this->access_token = $accessToken->getToken();
        $this->templateUrl = sprintf(Config('template.templateUrl'),$this->access_token['access_token']);
        $this->template_id = $template_id;
    }

    /**
     * 发送模板消息
     *
     * @param $info           openid,form_id/prepay_id
     * @param $sendData       模板内容
     * @return array|bool
     */
    public function sendMessage($info,$sendData){

        $templateData = [
            'touser' => $info['openid'], //发给谁
            'template_id' => $this->template_id,
            'form_id' => $info['form_id'],// 表单提交场景下，为 submit 事件带上的 formId；支付场景下，为本次支付的 prepay_id
            'page' =>$info['page'],//为空表示点击不跳转
            'data' => $sendData //模板内容，不填则下发空模板
        ];

        $result = https_request($this->templateUrl,json_encode($templateData));
        $teResult = json_decode($result,true);
        if($teResult['errcode'] == 0){
            return true;
        }else{
            return ['code'=>$teResult['errcode'],'msg'=>$teResult['errmsg']];
        }
    }


}